package com.jxc.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.googlecode.jsonplugin.annotations.JSON;
import com.jxc.entity.TbMenu;
import com.jxc.dao.TreeDAO;
import com.jxc.dao.permissionrole;
import com.jxc.pojo.TreeNode;

/**
 * @包名   com.jxc.web
 * @文件名 OpTreeAction.java
 * @作者   mopzyqing
 * @创建日期 Jul 16, 2010
 * @版本 V 1.0
 */
public class OpTreeAction {

	private String node;//前端传过来的参数，父节点的ID
	private List<TreeNode> JsonTreeNodeLst = new ArrayList<TreeNode>(); //返回给客户端的节点对象
	private int roleID;
	@Resource private TreeDAO treeImpl;
	@Resource private permissionrole permissionRoleService;
	public String queryMenu() {
		
		List<String> opMenuId = permissionRoleService.getRoleMenu(roleID);//已有权限的一级菜单和二级菜单ID
		
		List tbMenuList = treeImpl.QueryMenu1();
		for(int i=0;i<tbMenuList.size();i++){
			
			TbMenu tbMenu = (TbMenu)tbMenuList.get(i);
			TreeNode treeNode = new TreeNode();
			treeNode.setId(tbMenu.getMenuId().toString());
			treeNode.setParentid(node);//parentid
			treeNode.setText(tbMenu.getMenuName());//节点显示
			treeNode.setCls("");//图标
			treeNode.setLeaf(false);//是否叶子
			treeNode.setChildren(new ArrayList());//子节点
			if(opMenuId.indexOf(tbMenu.getMenuId())!=-1){
				treeNode.setChecked(true);
			}
			List tbMenuSubList = treeImpl.QueryMenuSubByID(tbMenu.getMenuId());
			for(int ii=0;ii<tbMenuSubList.size();ii++){
				TbMenu tbMenuSub = (TbMenu)tbMenuSubList.get(ii);
				TreeNode treeNodeSub = new TreeNode();
				treeNodeSub.setId(tbMenuSub.getMenuId().toString());
				treeNodeSub.setParentid(treeNode.getId());//parentid
				treeNodeSub.setText(tbMenuSub.getMenuName());//节点显示
				treeNodeSub.setCls("");//图标
				treeNodeSub.setLeaf(true);//是否叶子
				if(opMenuId.indexOf(tbMenuSub.getMenuId())!=-1){
					treeNodeSub.setChecked(true);
				}
				treeNode.getChildren().add(treeNodeSub);
			}
			JsonTreeNodeLst.add(treeNode);
		}
		
		return "success";
	}
	public String getNode() {
		return node;
	}

	public void setNode(String node) {
		this.node = node;
	}

	public List<TreeNode> getJsonTreeNodeLst() {
		return JsonTreeNodeLst;
	}

	public void setJsonTreeNodeLst(List<TreeNode> jsonTreeNodeLst) {
		JsonTreeNodeLst = jsonTreeNodeLst;
	}
	public int getRoleID() {
		return roleID;
	}
	public void setRoleID(int roleID) {
		this.roleID = roleID;
	}
}
